<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no,minimal-ui" name="viewport">
    <title>饱了吗</title>
    <link href="./../images/favico.ico" rel="icon">
    <!--不同屏幕尺寸根字体设置-->
    <script src="./../js/base.js"></script>
    <!--element-ui的样式-->
    <link href="../../backend/plugins/element-ui/index.css" rel="stylesheet" />
    <!--引入vant样式-->
    <link href="../styles/vant.min.css" rel="stylesheet" />
    <!-- 引入样式  -->
    <link href="../styles/index.css" rel="stylesheet" />
    <!--本页面内容的样式-->
    <link href="./../styles/address.css" rel="stylesheet" />
</head>

<body>
    <div class="app" id="address">
        <div class="divHead">
            <div class="divTitle">
                <i @click="goBack" class="el-icon-arrow-left"></i>地址管理
            </div>
        </div>
        <div class="divContent">
            <div :key="index" @click.capture="itemClick(item)" class="divItem" v-for="(item,index) in addressList">
                <div class="divAddress">
                    <span :class="{spanCompany:item.label === '公司',spanHome:item.label === '家',spanSchool:item.label === '学校'}">{{item.label}}</span> {{item.detail}}
                </div>
                <div class="divUserPhone">
                    <span>{{item.consignee}}</span>
                    <span>{{item.sex === '0' ? '女士' : '先生'}}</span>
                    <span>{{item.phone}}</span>
                </div>
                <img @click.stop.prevent="toAddressEditPage(item)" src="./../images/edit.png" />
                <div class="divSplit"></div>
                <div class="divDefault">
                    <img src="./../images/checked_true.png" v-if="item.isDefault === 1">
                    <img @click.stop.prevent="setDefaultAddress(item)" src="./../images/checked_false.png" v-else>设为默认地址
                </div>
            </div>
        </div>
        <div @click="toAddressCreatePage" class="divBottom">+ 添加收货地址</div>
    </div>
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="../../backend/plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../backend/plugins/element-ui/index.js"></script>
    <!-- 引入vant样式 -->
    <script src="./../js/vant.min.js"></script>
    <script src="./../api/address.js"></script>
    <!-- 引入axios -->
    <script src="../../backend/plugins/axios/axios.min.js"></script>
    <script src="./../js/request.js"></script>
    <script>
        new Vue({
            el: "#address",
            data() {
                return {
                    addressList: [
                        //     {
                        //     label:'公司',
                        //     detail:'金燕龙写字楼 4层电梯口fsdfdsfsdfsdfsssssssssssss（电梯可…）',
                        //     consignee:'周先生',//姓名
                        //     phone:18544445566,//手机号
                        //     isDefault:0,//是否是默认
                        // },
                        // {
                        //     label:'家',
                        //     detail:'金燕龙写字楼 4层电梯口（电梯可…）',
                        //     consignee:'周先生',//姓名
                        //     phone:18544445566,//手机号
                        //     isDefault:0,//是否是默认
                        // },
                        // {
                        //     label:'学校',
                        //     detail:'金燕龙写字楼 4层电梯口（电梯可…）',
                        //     consignee:'周先生',//姓名
                        //     phone:18544445566,//手机号
                        //     isDefault:0,//是否是默认
                        // },
                        // {
                        //     label:'公司',
                        //     detail:'金燕龙写字楼 4层电梯口（电梯可…）',
                        //     consignee:'周先生',//姓名
                        //     phone:18544445566,//手机号
                        //     isDefault:0,//是否是默认
                        // },
                        // {
                        //     label:'公司',
                        //     detail:'金燕龙写字楼 4层电梯口（电梯可…）',
                        //     consignee:'周先生',//姓名
                        //     phone:18544445566,//手机号
                        //     isDefault:0,//是否是默认
                        // },
                        // {
                        //     label:'公司',
                        //     detail:'金燕龙写字楼 4层电梯口（电梯可…）',
                        //     consignee:'周先生',//姓名
                        //     phone:18544445566,//手机号
                        //     isDefault:1,//是否是默认
                        // }
                    ],
                }
            },
            computed: {},
            created() {
                this.initData()
            },
            mounted() {},
            methods: {
                goBack() {
                    history.go(-1)
                },
                toAddressEditPage(item) {
                    window.requestAnimationFrame(() => {
                        window.location.href = `/front/page/address-edit.html?id=${item.id}`
                    })
                },
                toAddressCreatePage() {
                    window.requestAnimationFrame(() => {
                        window.location.href = '/front/page/address-edit.html'
                    })
                },
                async initData() {
                    const res = await addressListApi()
                    if (res.code === 1) {
                        this.addressList = res.data
                    } else {
                        this.$message.error(res.msg)
                    }
                },
                async setDefaultAddress(item) {
                    if (item.id) {
                        const res = await setDefaultAddressApi({
                            id: item.id
                        })
                        if (res.code === 1) {
                            this.initData()
                        } else {
                            this.$message.error(res.msg)
                        }
                    }
                },
                itemClick(item) {
                    const url = document.referrer
                        //表示是从订单页面跳转过来的
                    if (url.includes('order')) {
                        this.setDefaultAddress(item)
                        history.go(-1)
                    }
                }

            }
        })
    </script>
</body>

</html>